#include "GameObject.h"
#include "BaseEntity.h"
#include "Model.h"
#include "GlobalDefines.h"
#include <math.h>



BaseEntity::BaseEntity(int id):GameObject(id) 
{

}

void BaseEntity::setPosition(float a_x, float a_y, float a_z)
{
	m_position.x = a_x;
	m_position.y = a_y;
	m_position.z = a_z;
}

void BaseEntity::setVectorRotation(float a_x, float a_y, float a_z)
{
	m_VectorRotation.x = a_x;
	m_VectorRotation.y = a_y;
	m_VectorRotation.z = a_z;
}

void BaseEntity::setDirection(float a_x, float a_y, float a_z)
{
	m_direction.x = a_x;
	m_direction.y = a_y;
	m_direction.z = a_z;
	setTDirection();
	CrossProduct();
}

void BaseEntity::setTDirection()
{
	m_tDir.x = m_direction.x;
	m_tDir.y = m_direction.y;
	m_tDir.z = m_direction.z;
	rotatePerpendicular();
}

void BaseEntity::setMagnitude(float a_x, float a_y, float a_z)
{
	m_magnitude.x = a_x;
	m_magnitude.y = a_y;
	m_magnitude.z = a_z;
}

void BaseEntity::updateDelta()
{
	m_delta.x = m_direction.x*m_magnitude.x;
	m_delta.y = m_direction.y*m_magnitude.y;
	m_delta.z = m_direction.z*m_magnitude.z;
}

void BaseEntity::rotateVector()
{
	if(m_VectorRotation.z>=0.0)
	{
		m_direction.x=sin(D3DXToRadian(m_VectorRotation.z));
		m_direction.z=cos(D3DXToRadian(m_VectorRotation.z));
	}
	else if (m_VectorRotation.z>=360.0)//These checks prevent the rotations from becoming too large (or small)
	{
		m_VectorRotation.z-=360.0;
		m_direction.x=sin(D3DXToRadian(m_VectorRotation.z));
		m_direction.z=cos(D3DXToRadian(m_VectorRotation.z));
	}
	else if(m_VectorRotation.z<0.0)
	{
		m_VectorRotation.z+=360.0;
		m_direction.x=sin(D3DXToRadian(m_VectorRotation.z));
		m_direction.z=cos(D3DXToRadian(m_VectorRotation.z));

	}
	rotatePerpendicular();
	CrossProduct();
}

void BaseEntity::rotatePerpendicular()
{
	m_tDir.x=sin(D3DXToRadian(m_VectorRotation.z+90));
	m_tDir.z=cos(D3DXToRadian(m_VectorRotation.z+90));
}

float BaseEntity::polarConversion2D()
{
	return (D3DXToDegree(atan2(m_arc.x,m_arc.z)));
}

float BaseEntity::polarConversion2D(D3DXVECTOR3 a_point)
{
	return (D3DXToDegree(atan(a_point.x/a_point.z)));
}

D3DXVECTOR3 BaseEntity::localizePoint(D3DXVECTOR3 a_point)
{
	return a_point+=(m_position*-1);
}

void BaseEntity::CrossProduct()
{
	m_upDir.x=(m_direction.y*m_tDir.z-m_direction.z*m_tDir.y);
	m_upDir.y=(m_direction.z*m_tDir.x-m_direction.x*m_tDir.z);
	m_upDir.z=(m_direction.x*m_tDir.y-m_direction.y*m_tDir.x);
}

void BaseEntity::updateDrawRotation()
{
	//this updates the rotation that the model will be drawn at.
	//Maybe wrong, mathbook is utter shit. <.<
}


